//
//  DB.m
//  数据库的使用
//
//  Created by Qiaofeng on 15/5/11.
//  Copyright (c) 2015年 Qiaofeng. All rights reserved.
//

#import "DB.h"

// 确保数据库指针不管创建多少次,永远都是同一个指针;
static sqlite3 *dbPoint = nil;

@implementation DB

+ (sqlite3 *)openDB
{
    if (dbPoint) {
        return dbPoint;
    }
    //如果有就指针就返回,没有就创建;
    // 判断Library中是否有数据库文件,如果没有则拷贝进去,如果有则什么都不做.

    NSFileManager *manager = [NSFileManager defaultManager];

    NSArray *array = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *path = [array lastObject];
    path = [NSString stringWithFormat:@"%@/Novel.rdb", path];
    NSString *path1 = [[NSBundle mainBundle] pathForResource:@"Novel.rdb" ofType:nil];
    BOOL judge = [manager fileExistsAtPath:path];
    if (judge) {
        NSLog(@"有文件");
        NSLog(@"%@",path);
    } else {
        [manager copyItemAtPath:path1 toPath:path error:nil];
        
    }

    // 获得拷贝到得数据库文件指针
    
    sqlite3_open([path UTF8String], &dbPoint);
    // 返回dbpoint;
    return dbPoint;
    
}


+ (void)close
{
    sqlite3_close(dbPoint);
}
@end
